<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class news_types_model extends CI_Model
{  
    public function __construct()
    {
      parent::__construct();
    }
    
    public function dataSort($field, $order)
    {
      $xOrder =  $order == 'asc' ? 'desc' : 'asc';
      $img = $order == 'desc' ? '<img src=\'/data/admin/down.jpg\' />' : ' <img src=\'/data/admin/up.jpg\' />';  
      $url = anchor('news_types/sort/'.$field.'/'.$xOrder, t($field).$img);
      $_SESSION['sm_sort_news_types'] = array('field' => $field, 'order' => $order, 'url' => $url);
    }
    
    public function dataIndex($target)
    {
      $numshow = 25;
      $this->db->select('id, title, parent_id, status, created');
      $this->db->from('news_types');
      if ($this->session->userdata('sm_find_news_types')) {
        $this->db->like('news_types.title', $this->session->userdata('sm_find_news_types'));
      }
      if (isset($_SESSION['sm_sort_news_types'])) {
        $sort = $_SESSION['sm_sort_news_types'];
        $this->db->order_by('news_types.'.$sort['field'], $sort['order']);
       }
      else {  
        $this->db->order_by('news_types.created', 'desc');
      }
      $this->db->limit($numshow, $target);
      $query = $this->db->get();
      $paging = $this->home_model->paging_Admin('news_types/index', $this->_total(), $numshow, 4);
      return array('query'=>$query, 'paging'=>$paging,                    
                  'formFind' => $this->formFind());
    }
      
    private function _total()
    {
        return $this->db->count_all_results("news_types"); 
    }
    
    private function _checknews_typesById($id)
    {
        $query = $this->db->get_where("news_types", array("id" => $id));
        if ($query->num_rows() > 0) return true;
        else {
            notify_set_messages(notify_atten(t("Record không tồn tại")));
            return false;
        }
    }
    
    private function formFind()
    {
        $this->load->helper('form');
        $attributes = array('name' => 'frmFind', 'id' => 'frmFind');
        $f = form_open(site_url('news_types/find'), $attributes);
        $data = array('name' => 'txt_find', 'id' => 'txt_find', 'value' => '', 'style' => 'width:200px');
        $f .= form_input($data);
        $s = array('name' => 'submit', 'value' => t('Find'), 'class' => 'ibutton green', 'style' => 'margin-left:10px');
        $f .= form_submit($s);
        if ($this->session->userdata('sm_find_news_types')) {
            $r = array('name' => 'btnReset', 'value' => 'Reset', 'class' => 'ibutton red', 'style' => 'margin-left:10px');
            $f .= form_submit($r);
        } 
        $f .= form_close();
        return $f;
    }
    
    
    public function dataEdit($id)
    {
        if (!$this->_checknews_typesById($id)) return redirect(site_url('news_types'));
        $query = $this->db->get_where('news_types',array('id' => $id));
        return array('query' => $query
                    );
    }
    
    public function dataAdd()
    {
    }
    
    public function checkForm()
    {
        $this->form_validation->set_rules('title', t('title'),'trim|required');
        $this->form_validation->set_rules('alias', t('alias'),'trim|required');
        $this->form_validation->set_rules('order', t('order'),'trim|required|integer');
        $this->form_validation->set_rules('status', t('status'),'trim|required');
        return $this->form_validation->run();
    } 
    
    public function doUpdate()
    {
        $this->load->helper('text');
        $this->setTitle($this->input->post('title'));
        $this->setAlias($this->input->post('alias'));
        $this->setOrder($this->input->post('order'));
        $this->setStatus($this->input->post('status'));
            
        if (isset($_POST['idx'])) {
            $id = decrypt($_POST['idx']);
            $this->setId($id);
            $this->setChanged(time());
        }
        else {
            $this->setCreated(time());
        }
        return $this->save();
    }
    
}
